package com.davidfauthoux.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;

public class Resume implements EntryPoint {
	
	private static native boolean hasFlash() /*-{
		return $wnd.FlashDetect.installed;
	}-*/;
	private static native String getVideoId(String id) /*-{
		return $wnd.videos[id];
	}-*/;
	private static int getVideoInt(String id) {
		return Integer.parseInt(getVideoId(id));
	}
	private static final int VIDEO_WIDTH;
	private static final int VIDEO_HEIGHT;
	private static final boolean HAS_FLASH;
	static {
		VIDEO_WIDTH = getVideoInt("width");
		VIDEO_HEIGHT = getVideoInt("height");
		HAS_FLASH = hasFlash();
	}
	
	private static final class SwitchPlayer {
		private static SwitchPlayer currentButton = null;
		private ChromelessPlayer.Player player;
		private final Panel p;
		private int buttonState = 0;
		private final String videoId;
		private final Anchor launchButton;
		public SwitchPlayer(Anchor launchButton, String videoId) {
			this.launchButton = launchButton;
			this.videoId = videoId;
			p = RootPanel.get("video-insert-" + videoId);
			launchButton.addStyleName("launch-button");
		}
		public void launch() {
			if (currentButton != null) {
				currentButton.stop();
			}
			currentButton = this;
			
			buttonState = -1;
			player = ChromelessPlayer.register(VIDEO_WIDTH, VIDEO_HEIGHT, new ChromelessPlayer.Listener() {
				@Override
				public void onStateChange(ChromelessPlayer.State state) {
					if (state == ChromelessPlayer.State.READY) {
						String id = getVideoId(videoId);
						player.load(id, 0);
						buttonState = 1;
						launchButton.removeStyleName("launch-button");
						launchButton.addStyleName("stop-button");
					}
				}
			});
			DeferredCommand.addCommand(new Command() {
				@Override
				public void execute() {
					p.add(player.getWidget());
				}
			});
		}
		public void stop() {
			currentButton = null;
			buttonState = -1;
			player.stop();
			player.destroy();
			DeferredCommand.addCommand(new Command() {
				@Override
				public void execute() {
					buttonState = 0;
					launchButton.removeStyleName("stop-button");
					launchButton.addStyleName("launch-button");
					p.remove(player.getWidget());
				}
			});
		}
		public void switchState() {
			if (buttonState == 0) {
				launch();
			} else if (buttonState == 1) {
				stop();
			}
		}
	}
	
	private static Widget createButton(final String videoId) {
		if (!HAS_FLASH) {
			String id = getVideoId(videoId);
			Anchor launchButton = new Anchor("", "http://www.youtube.com/v/" + id);
			launchButton.addStyleName("launch-button");
			return launchButton;
		} else {
			final Anchor launchButton = new Anchor("", "javascript:void(0);"); // void(0); required to have the hover event on IE
			final SwitchPlayer switchPlayer = new SwitchPlayer(launchButton, videoId);
			launchButton.addClickHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					switchPlayer.switchState();
					launchButton.setFocus(false);
				}
			});
			return launchButton;
		}
	}
	
	private static void applyToElements(Element parent, String idPrefix) {
        if (parent != null) {
            int children = DOM.getChildCount(parent);
            for (int i = 0; i < children; i++) {
                Element child = DOM.getChild(parent, i);
                applyToElements(child, idPrefix);
                String id = child.getId();
                if (id.startsWith(idPrefix)) {
                	String suffix = id.substring(idPrefix.length());
                	RootPanel.get(id).add(createButton(suffix));
                }
            }
        }
    }

	public void onModuleLoad() {
		Element emailElement = RootPanel.get("email").getElement();
		String obfuscatedEmail = DOM.getInnerHTML(emailElement);
		DOM.setInnerHTML(emailElement, obfuscatedEmail.replace(" dot ", ".").replace(" at ", "@"));

		Element phoneElement = RootPanel.get("phone").getElement();
		String obfuscatedPhone = DOM.getInnerHTML(phoneElement);
		String phone = obfuscatedPhone.replace("zero", "0").replace("one", "1").replace("two", "2").replace("three", "3").replace("four", "4").replace("five", "5").replace("six", "6").replace("seven", "7").replace("eight", "8").replace("nine", "9");
		phone = phone.replaceAll("(.)\\-(.)", "$1$2");
		DOM.setInnerHTML(phoneElement, phone);
		
		applyToElements(RootPanel.getBodyElement(), "video-button-");
	}

}
